package io.github.harmonly.controller.api;

import io.github.harmonly.entity.Finance;
import io.github.harmonly.service.FinanceService;
import io.github.harmonly.util.JSONUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Controller
@RequestMapping("/api")
public class FinanceController {

    @Resource
    private FinanceService service;

    @ResponseBody
    @RequestMapping(value = "/finances", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    public String getFinances() {
        return JSONUtil.toJSONString(service.getFinances());
    }

    @ResponseBody
    @RequestMapping(value = "/finance/{fid}", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    public String getFinance(@PathVariable("fid") int fid) {
        return JSONUtil.toJSONString(service.getFinance(fid));
    }

    @ResponseBody
    @RequestMapping(value = "/finance", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.POST)
    public String addFinance(@RequestBody String json) {
        return JSONUtil.result(service.addFinance(JSONUtil.parseObject(json, Finance.class)));
    }

    @ResponseBody
    @RequestMapping(value = "/finance/{fid}", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.DELETE)
    public String deleteFinance(@PathVariable("fid") int fid) {
        return JSONUtil.result(service.deleteFinance(fid));
    }

    @ResponseBody
    @RequestMapping(value = "/finance", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.PUT)
    public String editFinance(@RequestBody String json) {
        return JSONUtil.result(service.editFinance(JSONUtil.parseObject(json, Finance.class)));
    }
}
